#include "Pit.h"
#include "Pic.h"
#include "Const.h"
#include "Irq.h"
#include "Table.h"
#include "utility/Types.h"
#include "utility/Instruction.h"

void HandleTimerException(void);

/// @brief initialize programmable interrupt timer
void InitPIT()
{
	// TODO: Initialize PIT
    uint32_t count = PIT_TIMER_FREQ / TICK_COUNT_PER_SECOND;
    Outb(PIT_COMMAND_MODE_PORT, PIT_CHANNLE0 | PIT_LOAD_LOHI | PIT_MODE3);
    Outb(PIT_CHANNEL0_DATA_PORT, count & 0xFF); //low bits
    Outb(PIT_CHANNEL0_DATA_PORT, (count >> 8) & 0xFF); // high bits

    SetIDTGateDesc(IRQ_TIMER, GDT_CODE_SELECTOR, (uint32_t)HandleTimerException, IDT_GATE_ATTR);
    EnablePicIrq(IRQ_TIMER);
}